﻿using System;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using RssReaderTemplate.ViewModels;
using System.Linq;
using Microsoft.Phone.Shell;
using RssReaderTemplate.Globalization;
using Microsoft.Phone.Reactive;

namespace RssReaderTemplate
{
    public partial class MainPage : PhoneApplicationPage
    {
        private ListViewModel _viewModel;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            BuildApplicationBar();
        }

        public void BuildApplicationBar()
        {
            ApplicationBarIconButton button = new ApplicationBarIconButton(new Uri("/icons/Categories.png", UriKind.Relative))
                {
                    Text = AppResources.Categories,
                };
            button.Click += Categories_Click;
            ApplicationBar.Buttons.Add(button);
            button = new ApplicationBarIconButton(new Uri("/icons/About.png", UriKind.Relative))
                {
                    Text = AppResources.About    
                };
            button.Click += About_Click;
            ApplicationBar.Buttons.Add(button);
        }

        private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = _viewModel.SelectedItemIndex;
            var id = _viewModel.FeedItems.ElementAt(index).Id.ToString();
            NavigationService.Navigate(new Uri(string.Format("/Views/Details.xaml?index={0}&id={1}", index, id), UriKind.Relative));
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            this.State["ViewModel"] = _viewModel;
            Items.SelectionChanged -= this.Items_SelectionChanged;
            _viewModel.Dispose();
            base.OnNavigatingFrom(e);
        }

        private void ApplicationBarVisibility(bool networkConectivity)
        {
            ApplicationBar.IsVisible = networkConectivity;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            object viewModel;
            if (!State.TryGetValue("ViewModel", out viewModel))
            {
                _viewModel = ViewModelCollection.ListViewModel;
                _viewModel.NetworkMonitor.ObserveOnDispatcher().Subscribe(ApplicationBarVisibility);
            }
            else
            {
                _viewModel = viewModel as ListViewModel;
            }
            DataContext = _viewModel;
            Items.SelectionChanged += this.Items_SelectionChanged;
            base.OnNavigatedTo(e);
        }

        private void Categories_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Categories.xaml", UriKind.Relative));
        }

        private void About_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative));
        }
    }
}